package com.cty.B_SpecialDataStructures.A_Stack.StackChain;

import com.cty.F_common.DataItem;

/**
 * @Auther: cty
 * @Date: 2020/6/13 22:52
 * @Description: 单链表实现栈
 * 应用场景：
 *      数据量不可预测时使用
 * 重要操作：
 *      ① peek(): DataItem    O(1)
 *      ② push(DataItem): void    O(1)
 *      ③ pop(): DataItem    O(1)
 * @version: 1.0
 */
public class StackChain {
    private LinkedList list;

    public StackChain(){
        list = new LinkedList();
    }

    public boolean isEmpty(){
        return list.isEmpty();
    }

    public void displayStack(){
        list.displayList();
    }

    /**
     * 查看栈顶  O(1)
     * @return
     *      DataItem
     *      null
     */
    public DataItem peek(){
        if(isEmpty())
            return null;

        return list.getFirst().data;
    }

    /**
     * 压栈  O(1)
     * @param item
     */
    public void push(DataItem item)
    {
        list.insertFirst(item);
    }

    /**
     * 出栈  O(1)
     * @return
     *      DataItem
     *      null
     */
    public DataItem pop(){
        return list.deleteFirst();
    }

}  // end StackChain
